package autohotkeyide;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JDialog;
import javax.swing.Timer;

/**
 * @author sad
 */
public class SelectionWindow extends JDialog {

    enum CurrentState {

        RectangleSelection, HotSpotSelection
    }
    private CurrentState currentState = CurrentState.RectangleSelection;
    private BufferedImage background;
    private int startMouseX = -1;
    private int startMouseY = -1;
    private int mouseX = -1;
    private int mouseY = -1;
    private Point hotSpot;
    private boolean modalResult = false;

    public static class SelectionResult {

        public BufferedImage selected;
        public Point hotspot;
    };

    public void setBackground(BufferedImage background) {
        this.background = background;
    }

    public SelectionWindow(Window window) {
        super(window, ModalityType.APPLICATION_MODAL);
        setUndecorated(true);
        setAlwaysOnTop(true);
        setModal(true);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(screenSize.width, screenSize.height);
        setLocation(0, 0);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    setVisible(false);
                    dispose();
                }
            }
        });

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (currentState == CurrentState.RectangleSelection) {
                    startMouseX = e.getX();
                    startMouseY = e.getY();
                    currentState = CurrentState.HotSpotSelection;
                } else {
                    hotSpot = new Point();
                    hotSpot.x = e.getX() - startMouseX;
                    hotSpot.y = e.getY() - startMouseY;
                    repaint();
                    final Timer timer = new Timer(500, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            modalResult = true;
                            setVisible(false);
                            dispose();
                        }
                    });
                    timer.setRepeats(false);
                    timer.start();
                }
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                mouseX = e.getX();
                mouseY = e.getY();
                repaint();
            }
        });
    }

    public SelectionResult selectRegion() {
        setVisible(true);
        if (modalResult == true) {
            SelectionResult selectionResult = new SelectionResult();
            selectionResult.hotspot = hotSpot;
            selectionResult.selected = background.getSubimage(startMouseX, startMouseY, mouseX - startMouseX, mouseY - startMouseY);
            return selectionResult;
        }
        return null;
    }

    @Override
    public void paint(Graphics g) {
        //super.paint(g);
        if (background != null) {
            g.drawImage(background, 0, 0, this);
        }
        g.setColor(Color.BLUE);
        //g.setXORMode(Color.BLUE);

        if (startMouseX > mouseX) {
            int t = startMouseX;
            startMouseX = mouseX;
            mouseX = t;
        }

        if (startMouseY > mouseY) {
            int t = startMouseY;
            startMouseY = mouseY;
            mouseY = t;
        }

        int width = mouseX - startMouseX;
        int height = mouseY - startMouseY;
        if (width != 0 && height != 0) {
            g.drawRect(startMouseX, startMouseY, width, height);
            if ((width - 2) > 0 && (height - 2) > 0) {
                g.drawRect(startMouseX + 1, startMouseY + 1, width - 2, height - 2);
            }
            if (hotSpot != null) {
                g.drawLine(startMouseX + hotSpot.x - 10, startMouseY + hotSpot.y, startMouseX + hotSpot.x + 10, startMouseY + hotSpot.y);
                g.drawLine(startMouseX + hotSpot.x, startMouseY + hotSpot.y - 10, startMouseX + hotSpot.x, startMouseY + hotSpot.y + 10);
            }
        }
        //g.setPaintMode();
    }
}
